/**
 * Project Name:meatball-admin
 * File Name:UserServiceImpl.java
 * Package Name:com.meatball.base.system.user.service.impl
 * Date:2018年1月13日下午2:30:23
 * Copyright (c) 2018, zhang.xiangyu@foxmail.com All Rights Reserved.
*/
package com.meatball.base.system.user.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.meatball.base.system.user.params.SysUserParams;
import com.meatball.base.system.utils.SystemUtil;
import com.meatball.constant.MeatballConst;
import com.meatball.utils.DateUtil;
import com.meatball.utils.RSAUtils;
import com.meatball.utils.UUIDUtil;
import com.meatball.vo.PageResultMsg;
import com.meatball.vo.RSAKeys;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.meatball.base.system.role.dao.SysRoleMapper;
import com.meatball.base.system.role.dao.SysUserRoleLinksMapper;
import com.meatball.base.system.role.model.SysRole;
import com.meatball.base.system.user.dao.SysUserMapper;
import com.meatball.base.system.user.model.SysUser;
import com.meatball.base.system.user.service.SysUserService;
import com.meatball.utils.MD5Util;
import com.meatball.vo.ResultMsg;

/**   
 * @Title: UserServiceImpl.java 
 * @Package com.meatball.base.system.user.service.impl
 * @Description: TODO(用户管理) 
 * @author 張翔宇  
 * @date 2018年1月13日 下午2:30:23 
 * @version V1.0   
 */
@Service
public class SysUserServiceImpl implements SysUserService {
	@Resource
	private SysUserMapper mapper;
	@Resource
	private SysRoleMapper roleMapper;
	@Resource
	private SysUserRoleLinksMapper linksMapper;

	@Override
	public PageResultMsg table(SysUser record) {
		SysUser loginUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute(MeatballConst.SYSTEM_USER);
		record.setLoginUser(loginUser.getAccount());
		PageInfo<SysUser> list = PageHelper.startPage(record.getOffset(), record.getLimit()).doSelectPageInfo(() -> mapper.selectList(record));
		return new PageResultMsg(MeatballConst.ResultCode.SUCCESS.CODE, list.getTotal(), list.getList());
	}


	@Override
	public ResultMsg add(SysUser record, Long[] roles) {
		int status  = mapper.selectByAccount(record.getAccount());
		if(status > MeatballConst.ZERO) {
			return new ResultMsg(MeatballConst.ResultCode.ACCOUNT_EXIST.CODE, MeatballConst.ResultCode.ACCOUNT_EXIST.MESSAGE);
		} else {
			String salt = MD5Util.getRandomString(MeatballConst.SALT_LEGTH);
			record.setPassword(MD5Util.md5(record.getPassword(), salt));
			record.setSalt(salt);
			record.setStatus(MeatballConst.ONE);
			record.setCreatetime(new Date());
			record.setAppid(SystemUtil.createAppid(salt));
			// 设置调用密码
			record.setAppsecret(MD5Util.md5(UUIDUtil.uuid(), salt));
			mapper.insertSelective(record);
			// 保存与角色关联信息
			if(roles[MeatballConst.ZERO] != -MeatballConst.NEGATIVE_ONE) {
				linksMapper.insertUserRoleLinks(record.getId(), roles);
			}
			return new ResultMsg(MeatballConst.ResultCode.SUCCESS.CODE, MeatballConst.ResultCode.SUCCESS.MESSAGE);
		}
		
	}
	
	@Override
	public SysUser info(Long id) {
		SysUser sysUser = mapper.selectByPrimaryKey(id);
		List<SysRole> roles = roleMapper.selectRoleByUserId(id);
		sysUser.setRolesVo(roles);
		return sysUser;
	}
	
	@Override
	public ResultMsg update(SysUser record, Long[] roles) {
		String msg = "";
		if(record.isResetPwd()) {
			String salt = MD5Util.getRandomString(MeatballConst.SALT_LEGTH);
			record.setPassword(MD5Util.md5(MeatballConst.DEFAULT_PASSWORD, salt));
			record.setSalt(salt);
			msg += MeatballConst.DEFAULT_PASSWORD_MESSAGE;
		}
		mapper.updateByPrimaryKeySelective(record);
		// 删除角色关联信息
		linksMapper.deleteUserRoleLinksByUserId(record.getId());
		// 新增关联信息
		if(roles[MeatballConst.ZERO] != MeatballConst.NEGATIVE_ONE) {
			linksMapper.insertUserRoleLinks(record.getId(), roles);
		}
		return new ResultMsg(MeatballConst.ResultCode.SUCCESS.CODE, MeatballConst.ResultCode.SUCCESS.MESSAGE + msg);
	}
	
	@Override
	public ResultMsg delete(Long id) {
		mapper.deleteByPrimaryKey(id);
		// 删除角色关联信息
		linksMapper.deleteUserRoleLinksByUserId(id);
		return new ResultMsg(MeatballConst.ResultCode.SUCCESS.CODE, MeatballConst.ResultCode.SUCCESS.MESSAGE);
	}

	@Override
	public ResultMsg changePassword(SysUserParams record) {
		SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute(MeatballConst.SYSTEM_USER);
		// 转换密码为MD5
		String oldPsd = MD5Util.md5(record.getOldPsd(), sysUser.getSalt());
		// 对比原始密码是否相等
		if(sysUser.getPassword().equals(oldPsd)) {
			String salt = MD5Util.getRandomString(MeatballConst.SALT_LEGTH);
			sysUser.setSalt(salt);
			sysUser.setPassword(MD5Util.md5(record.getNewPsd(), salt));
			mapper.updateByPrimaryKeySelective(sysUser);
		} else {
			return new  ResultMsg(MeatballConst.ResultCode.ORIGINAL_PASSWORD_ERROR.CODE,
					MeatballConst.ResultCode.ORIGINAL_PASSWORD_ERROR.MESSAGE);
		}
		return new ResultMsg(MeatballConst.ResultCode.PASSWORD_CHANGED_SUCCESSFULLY.CODE,
				MeatballConst.ResultCode.PASSWORD_CHANGED_SUCCESSFULLY.MESSAGE);
	}

}
